package com.lalameow.serverlogingate.network.server;

import com.lalameow.packet.BasePack;
import com.lalameow.packet.packImpl.PacketAbstract;
import com.lalameow.serverlogingate.network.ClientManager;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 陈刚
 * 日期: 2018/5/7
 * 时间: 23:07
 * 功能：请进行修改
 */
@Slf4j
public class NettyServerHandler extends SimpleChannelInboundHandler<BasePack> {




    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        log.info("客户端{}已经连接服务器",ctx.channel().remoteAddress().toString());
        ClientManager.instanss.invaliPlayerCache(ctx.channel());

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
      //  super.exceptionCaught(ctx, cause);
        log.info("客户端{}断开了服务器",ctx.channel().remoteAddress().toString());
      //  InetSocketAddress client=(InetSocketAddress)ctx.channel().remoteAddress();
        //todo 玩家移除队列
        ClientManager.instanss.invaliPlayerCache(ctx.channel());
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, BasePack msg) throws Exception {
        


        PacketAbstract packet=msg.getPackType().getObject(msg.getBodyContent());
        BasePack recive= packet.revice(ctx);
        if(recive!=null){
            ctx.channel().writeAndFlush(recive);
        }
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
    }
}
